/*
 * PreviewBoard.java
 *
 * This work is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License as published 
 * by the Apache Software Foundation.
 *
 * This work is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * Apache License for more details.
 *
 * Copyright (c) 2011 Tran Hoang Anh (tranhoanganh89@gmail.com). 
 * All rights reserved.
 */
package net.hoanganh.jbraintetris;

import java.awt.Point;

/**
 * The board is used to paint the next piece in tetris game.
 */
@SuppressWarnings("serial")
public class PreviewBoard extends SquareBoard {

	/**
	 * Create new preview board.
	 * 
	 * @param width
	 * 			width in squares
	 * 
	 * @param height
	 * 			height in squares
	 */
	public PreviewBoard(int width, int height) {
		super(width, height);
	}
	
	/**
	 * Paint tetris piece.
	 * 
	 * @param piece
	 * 			piece to paint
	 * 
	 * @return false if can not paint
	 */
	public boolean paintPiece(Piece piece) {
		// Check piece
		if (piece == null) {
			return false;
		}
		
		// Reset board and piece location
		clear();
		int x = getBoardSize().width / 2;
		int y = getBoardSize().height / 2;

		// Paint piece
		Point[] positions = piece.getPositions();
		for (Point p : positions) {
			int squareX = x + p.x;
			int squareY = y + p.y;
			if (squareX < 0 || squareX >= getBoardSize().width || 
					squareY < 0 || squareY >= getBoardSize().height) {
				clear();
				return false;
			}
			
			setSquareColor(squareX, squareY, piece.getColor());
		}
		redraw();
				
		return true;
	}
}
